package com.levelup.cr.dao;

import com.levelup.cr.model.Node;
import com.levelup.cr.model.NodeProxy;
import com.levelup.cr.model.entity.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: denis_zavadsky
 * Date: 9/5/13
 * Time: 6:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class Main {

    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-dao.xml");
        Node root = createTestRootNode();
        Node node = createTestNode(root);
        NodeWriter nodeWriter = (NodeWriter) context.getBean("nodeWriter");
        nodeWriter.writeNode(root);
        nodeWriter.writeNode(node);

        NodeReader nodeReader = (NodeReader) context.getBean("nodeReader");
        Node test = nodeReader.readNodeByPath(node.getParentPath()+ File.separator + node.getName());
        NodeProxy parent = (NodeProxy)test.getParent();
        Node initedParent = parent.initialize();
    }

    private static Node createTestRootNode(){
        Node node = new Node();
        node.setName("level1");
        node.setRoot(true);
        node.getProperties().put(Node.NODE_TYPE_PROPERTY,"cr:Product");
        node.getProperties().put(Product.PROPERTY_NAME,"Product 1");
        node.getProperties().put(Product.PROPERTY_DESC,"Description 1");

        return node;
    }

    private static Node createTestNode(Node parent){
        Node node = new Node();
        node.setName("level2");
        node.setRoot(false);
        node.setParent(parent);
        node.getProperties().put(Node.NODE_TYPE_PROPERTY,"cr:Product");
        node.getProperties().put(Product.PROPERTY_NAME,"Product 2");
        node.getProperties().put(Product.PROPERTY_DESC,"Description 2");
        parent.addChild(node);
        return node;
    }
}
